﻿namespace OleDbDAL.Stock
{
    using DBUtility;
    using IDal.Stock;
    using Model.Stock;
    using System;
    using System.Data;
    using System.Text;

    public class InventoryCommApp : IInventoryComm
    {
        private string dbname = "Systems";

        public void Add(InventoryCommEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_InventoryComm(");
            builder.Append("LotNum,SupplierId,InventoryId,CommId,CommName,DlId,AgoUnit,AgoMoney,AlertMoney,AgoCount,AfterCount,ProfitMoney,DiffReason");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("'" + model.LotNum + "',");
            builder.Append("'" + model.SupplierId + "',");
            builder.Append("" + model.InventoryId + ",");
            builder.Append("" + model.CommId + ",");
            builder.Append("'" + model.CommName + "',");
            builder.Append("" + model.DlId + ",");
            builder.Append("" + model.AgoUnit + ",");
            builder.Append("" + model.AgoMoney + ",");
            builder.Append("" + model.AlertMoney + ",");
            builder.Append("" + model.AgoCount + ",");
            builder.Append("" + model.AfterCount + ",");
            builder.Append("" + model.ProfitMoney + ",");
            builder.Append("'" + model.DiffReason + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int InventoryCommId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_InventoryComm ");
            builder.Append(" where InventoryCommId=" + InventoryCommId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_InventoryComm ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by InventoryCommId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "InventoryCommId", "T_InventoryComm", swhere);
        }

        public InventoryCommEnt GetModel(int InventoryCommId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select InventoryCommId,LotNum,SupplierId,InventoryId,CommId,CommName,DlId,AgoUnit,AgoMoney,AlertMoney,AgoCount,AfterCount,ProfitMoney,DiffReason from T_InventoryComm ");
            builder.Append(" where InventoryCommId=" + InventoryCommId);
            InventoryCommEnt ent = new InventoryCommEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.InventoryCommId = InventoryCommId;
            if (table.Rows.Count > 0)
            {
                ent.LotNum = table.Rows[0]["LotNum"].ToString();
                ent.SupplierId = table.Rows[0]["SupplierId"].ToString();
                if (table.Rows[0]["InventoryId"].ToString() != "")
                {
                    ent.InventoryId = int.Parse(table.Rows[0]["InventoryId"].ToString());
                }
                if (table.Rows[0]["CommId"].ToString() != "")
                {
                    ent.CommId = int.Parse(table.Rows[0]["CommId"].ToString());
                }
                ent.CommName = table.Rows[0]["CommName"].ToString();
                if (table.Rows[0]["DlId"].ToString() != "")
                {
                    ent.DlId = int.Parse(table.Rows[0]["DlId"].ToString());
                }
                if (table.Rows[0]["AgoUnit"].ToString() != "")
                {
                    ent.AgoUnit = int.Parse(table.Rows[0]["AgoUnit"].ToString());
                }
                if (table.Rows[0]["AgoMoney"].ToString() != "")
                {
                    ent.AgoMoney = decimal.Parse(table.Rows[0]["AgoMoney"].ToString());
                }
                if (table.Rows[0]["AlertMoney"].ToString() != "")
                {
                    ent.AlertMoney = decimal.Parse(table.Rows[0]["AlertMoney"].ToString());
                }
                if (table.Rows[0]["AgoCount"].ToString() != "")
                {
                    ent.AgoCount = decimal.Parse(table.Rows[0]["AgoCount"].ToString());
                }
                if (table.Rows[0]["AfterCount"].ToString() != "")
                {
                    ent.AfterCount = decimal.Parse(table.Rows[0]["AfterCount"].ToString());
                }
                if (table.Rows[0]["ProfitMoney"].ToString() != "")
                {
                    ent.ProfitMoney = decimal.Parse(table.Rows[0]["ProfitMoney"].ToString());
                }
                ent.DiffReason = table.Rows[0]["DiffReason"].ToString();
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_InventoryComm ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "InventoryCommId");
        }

        public void Update(InventoryCommEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_InventoryComm set ");
            builder.Append("LotNum='" + model.LotNum + "',");
            builder.Append("SupplierId='" + model.SupplierId + "',");
            builder.Append("InventoryId=" + model.InventoryId + ",");
            builder.Append("CommId=" + model.CommId + ",");
            builder.Append("CommName='" + model.CommName + "',");
            builder.Append("DlId=" + model.DlId + ",");
            builder.Append("AgoUnit=" + model.AgoUnit + ",");
            builder.Append("AgoMoney=" + model.AgoMoney + ",");
            builder.Append("AlertMoney=" + model.AlertMoney + ",");
            builder.Append("AgoCount=" + model.AgoCount + ",");
            builder.Append("AfterCount=" + model.AfterCount + ",");
            builder.Append("ProfitMoney=" + model.ProfitMoney + ",");
            builder.Append("DiffReason='" + model.DiffReason + "'");
            builder.Append(" where InventoryCommId=" + model.InventoryCommId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

